<template>
  <el-dialog
    :title="form.id ? lang.t('common.update') : lang.t('common.create')"
    v-model="show"
    :width="500"
  >
    <el-form ref="formRef" :model="form" :rules="rules" label-position="top">
      <el-form-item :label="plugin.lang('tax.name')" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item :label="plugin.lang('tax.iva')" prop="iva">
            <el-input v-model="form.iva" @input="onLaw">
              <template #append>%</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="plugin.lang('tax.req')" prop="req">
            <el-input v-model="form.req">
              <template #append>%</template>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item v-if="law" :label="plugin.lang('tax.law')" prop="law">
        <el-select v-model="form.law" placeholder=" " style="width: 100%">
          <template v-for="(item, index) in plugin.langTm('tax.law_text')">
            <el-option
              v-if="item"
              :key="index"
              :label="item"
              :value="index"
            />
          </template>
        </el-select>
      </el-form-item>
      <el-form-item :label="plugin.lang('tax.sort')" prop="sort">
        <div style="width: 100%; overflow: hidden">
          <el-input-number v-model="form.sort" :min="0" />
        </div>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" :loading="loading" @click="onUpdate">{{ form.id ? lang.t('common.update') : lang.t('common.create') }}</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script>
import { ref } from 'vue'
import app from '@/app'
import taxModel from '../../model/tax'

export default {
  setup() {
    // 对话框
    const show = ref(false)

    // 表单
    const formRef = ref(null)
    const form = ref({})

    // 验证
    const rules = {
      name: [
        { required: true, message: ' ', trigger: 'blur' }
      ],
      iva: [
        { required: true, message: ' ', trigger: 'blur' }
      ],
      req: [
        { required: true, message: ' ', trigger: 'blur' }
      ],
      sort: [
        { required: true, message: ' ', trigger: 'blur' }
      ],
    }

    // 法律条例状态
    const law = ref(false)
    const onLaw = () => {
      if(form.value.iva && Number(form.value.iva) == 0) {
        law.value = true
        form.value.law = form.value.law > 0 ? form.value.law : 0
      } else {
        law.value = false
        form.value.law = 0
      }
    }

    // 打开
    const open = data => {
      form.value = Object.assign({}, data)
      show.value = true

      onLaw()
    }

    // 更新
    const loading = ref(false)
    const onUpdate = () => {
      formRef.value.validate(valid => {
        if (valid) {
          loading.value = true
          taxModel.update(form.value).then(() => {
            show.value = false
          }).finally(() => {
            loading.value = false
          })
        }
      })
    }

    return {
      lang: app.lang,
      plugin: app.plugin.index,
      show,
      open,
      formRef,
      form,
      rules,
      law,
      onLaw,
      loading,
      onUpdate
    }
  }
}
</script>